package com.starsoft.frame.core.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.util.DateUtil;
import com.starsoft.frame.util.StringUtil;

public class WebUtil {

	public static Map<String,Object> getParam(ServletRequest request){
		Map<String,Object> result=new HashMap<String,Object>();
		Enumeration<String> names =request.getParameterNames();
		while(names.hasMoreElements()){
			String name=names.nextElement();
			result.put(name, request.getParameter(name));
		}
		return result;
	}
	
	public static Map<String, Object> mapResult(String key, Object value){
		Map<String, Object> mm=new HashMap<>();
		mm.put(key, value);
		return mm;
	}
	
	public static Map<String, Object> errorModel(String msg){
		Map<String, Object> mm=new HashMap<>();
		mm.put("sucesse", false);
		mm.put("message", msg);
		return mm;
	}
	
	public static Map<String, Object> sucesseModel(String msg){
		Map<String, Object> mm=new HashMap<>();
		mm.put("sucesse", true);
		mm.put("message", msg);
		return mm;
	}
	
	public static PageObject getPageObject(ServletRequest request){
		int pageNo = StringUtil.parseInt(request.getParameter("page"), 1);
		int pageSize = StringUtil.parseInt(request.getParameter("rows"), 1);
		String sort = (String) request.getParameter("sort");
		String order = (String) request.getParameter("order");
		PageObject po = new PageObject(pageNo, pageSize);
		po.setSort(sort);
		po.setOrder(order);
		return po;
	}

	public static <T> List<T> getJsonList(ServletRequest request, String key, Class<T> entityClass){
		String content = request.getParameter(key);
		ObjectMapper mapper = new ObjectMapper(); 
		JavaType javaType = mapper.getTypeFactory().constructParametrizedType(ArrayList.class, List.class, entityClass);
		try {
			List<T> result = mapper.readValue(content, javaType);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public static List<Map<String, String>> getJsonList(ServletRequest request, String key){
		String content = request.getParameter(key);
		ObjectMapper mapper = new ObjectMapper(); 
		List<Map<String, String>> result = new ArrayList<>();
		if(StringUtil.isEmpty(content)){
			return result;
		}
		try {
			result = mapper.readValue(content, result.getClass());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, String> getJsonMap(ServletRequest request, String key){
		String content = request.getParameter(key);
		ObjectMapper mapper = new ObjectMapper(); 
		Map<String, String> result = new HashMap<>();
		if(StringUtil.isEmpty(content)){
			return result;
		}
		try {
			result = mapper.readValue(content, result.getClass());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Date getDate(ServletRequest request, String key, String format){
		String dateStr = request.getParameter(key);
		if(!StringUtil.isEmpty(dateStr)){
			return DateUtil.toDate(dateStr, format);
		}
		return null;
	}
	

	public static List<String> getList(ServletRequest request, String key){
		String content = request.getParameter(key);
		if(StringUtil.isEmpty(content)){
			return new ArrayList<String>();
		}
		content = content.trim();
		if(content.startsWith(",")){
			content = content.substring(1);
		}
		if(content.endsWith(",")){
			content = content.substring(0, content.length());
		}
		String[] splitContent = content.split(",");
		return Arrays.asList(splitContent);
	}
	
}
